import wineAreas
wineWorld = levinCommon.defaultWorld

from kivy.app import App
from kivy.uix.widget import Widget

from kivyext.treegridview import TreeView, TreeViewLabel, TreeViewNode, ScrollableHeaderTreeView
from kivyext.mylabel import MyLabel

from kivy.core.window import Window

#Window.clearcolor = (1,1,1,1)

from kivy.uix.label import Label
from kivy.properties import BooleanProperty, ListProperty, ObjectProperty

class LeVinWindowStyle(Widget):
    clearcolor = ListProperty([1,1,1,1])

class LeVinTreeLabel(Label):
    #robj = ObjectProperty(None, allownone=True)
    bkg_color = ListProperty([0, 0, 0, 0])
    
    
    def __init__(self, **kargs):
        #print 'MyLAbel ', kargs
        super(LeVinTreeLabel, self).__init__(**kargs)

        self.shorten = True
    pass

class LeVinAreaLabel(LeVinTreeLabel):
    pass

from kivy.lang import Builder
#guiCommon.load_kv_file('kivyext/mylabel.kv')

guiCommon.load_kv_file('kivyext/mytreeview.kv')
guiCommon.load_kv_file('levintreeview.kv')

Window.clearcolor = (1,1,1,1)#LeVinWindowStyle.clearcolor

NCOLS = 3
class LeVinAreaNode(TreeViewNode):

    def re_init(self):
        super(LeVinAreaNode,self).__init__(self)
        self.row_span = [0,NCOLS]
        self.columns = [LeVinAreaLabel(text=self.name,halign='center') ]
        self.setVisibility()

    def setVisibility(self):
        self.is_visible = self.nWinesTotal > 0
        
class LeVinBottleNode(TreeViewNode):
    def re_init(self):
        super(LeVinBottleNode,self).__init__(self)
        self.columns = [LeVinTreeLabel(text=self.appellation),LeVinTreeLabel(text=self.cru_name),LeVinTreeLabel(text=self.millesime)]

wineAreas.Area.__bases__ = (LeVinAreaNode,) + wineAreas.Area.__bases__
#wineAreas.CountryArea.__bases__ = (LeVinAreaNode,) + wineAreas.Area.__bases__ 
wineAreas.Bottle.__bases__ = (LeVinBottleNode,)+wineAreas.Bottle.__bases__ 


wineAreas.loadAll()

wineAreas.wineWorld.countNWines()
wineAreas.wineWorld.doOnSubAreas( LeVinAreaNode.re_init)
for b in wineAreas.wineWorld.getWines():
    b.re_init()
#wineAreas.wineWorld.doOnSubAreas( LeVinAreaNode.setVisibility)


theTree = ScrollableHeaderTreeView(size = Window.size, size_hint = (1,1) , num_columns = NCOLS, spacing_x=2,show_slider = True,
                             #columns_width_force=[ None,None,None],
                             header_content = ["H"+str(i) for i in xrange(NCOLS)], hide_header = False,
                             )
theTree._root = wineAreas.wineWorld
theTree._root.setup(theTree)
theTree.indent_level = 0
theTree.hide_root = True

def addRegionNode(self):
    if self.parentArea :
        theTree.add_node(self,parent = self.parentArea)
    for w in self.wines:
        theTree.add_node(w,parent= self)

def addAppellationNode(self):
    reg = self.getRegionOrAbove()
    if reg :
        for w in self.wines:
            theTree.add_node(w,parent= reg)
    

_addnodeFunc = {
    wineAreas.WorldArea : addRegionNode,
    wineAreas.CountryArea : addRegionNode,
    wineAreas.RegionArea : addRegionNode,
    wineAreas.AppellationArea : addAppellationNode,
    wineAreas.GroupAppellationArea : addAppellationNode    

    }

def addNode(self):
    _addnodeFunc[self.__class__ ]( self)
    
wineAreas.wineWorld.doOnSubAreas( addNode )



# define the app ---------------------
class MyPaintApp(App):
    theTree = None
    def build(self):
        return self.theTree

ap=MyPaintApp()
ap.theTree = theTree
# ------------------------------------


if __name__ == '__main__':
    #interactiveKivy.setupInteractive( ap, locals())
    ap.run()

